﻿// Copyright (c) 2012 Andrei Rinea
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Net;

using BenchmarkNet;

namespace BenchmarkNet.Samples
{
    //class with Benchmarked methods
    public class DoWork
    {
        [BenchmarkedThis(BenchmarkType.Sequential,"12/04/2010", 100, "00:00:00.0023", 15754, "First Benchmark after initial commit;")]
        [BenchmarkedThis(BenchmarkType.Sequential,"14/04/2010",100,"00:00:00.0018",15746,"Second Benchmark after first improvement set;")]
        [BenchmarkedThis(BenchmarkType.Parallel, "12/04/2010", 100, "00:00:00.0009", 15759, "First parallel Benchmark test.")]
        public static int DoALotOfWork()
        {
            int j = 0;
            for (int i = 0; i < 1000; i++)
            {
                System.Threading.Thread.Sleep(100);
                j += i;
            }
            return j;
        }

    }

    internal class Program
    {
        private static void Main()
        {
            Console.WriteLine("Benchmark sample");
            Console.WriteLine();

            Func<WebClient> func = () => new WebClient();

            var inst1 = Benchmark.Sequential(() => func(), 1000);
            var inst2 = Benchmark.Sequential(() => Activator.CreateInstance<WebClient>(), 1000);

            Console.WriteLine("Instantiate WebClient via Action : " + inst1);
            Console.WriteLine("Instantiate WebClient via Activator : " + inst2);

            Console.WriteLine();
            Console.WriteLine("--------------------------");
            Console.WriteLine();

            var dl1 = Benchmark.Sequential(() => new WebClient().DownloadString("http://localhost/"), 100);
            var dl2 = Benchmark.Parallel(new BenchmarkParams<WebClient>(c => c.DownloadString("http://localhost/"), 100), 10, () => new WebClient());

            Console.WriteLine("Download sequentially root site page : " + dl1);
            Console.WriteLine("Download parallel root site page : " + dl2);

            Console.WriteLine();
            Console.WriteLine("Press a key to exit.");
            Console.ReadKey();
        }
    }
}